package com.cpe.web.controller.sharing;

import static com.sql.data.domain.user.UserRespCode.INVALID_TOKEN;
import static com.sql.data.response.CommonRespCode.EXCEPTION;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpe.data.domain.member.Member;
import com.cpe.data.domain.sharing.Sharing;
import com.cpe.data.domain.sharing.SharingForm;
import com.cpe.data.domain.sharing.SharingQuery;
import com.cpe.repository.mapper.member.MemberMapper;
import com.cpe.repository.mapper.sharing.SharingMapper;
import com.cpe.service.file.FileUploadService;
import com.cpe.service.sharing.SharingService;
import com.cpe.transfer.sharing.SharingTransfer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sql.data.response.JsonResponse;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/sharings")
public class SharingController {

	@Resource
	private SharingService sharingService;

	@Resource
	private SharingMapper sharingMapper;

	@Resource
	private MemberMapper memberMapper;

	@Resource
	private SharingTransfer sharingTransfer;

	@Resource
	private FileUploadService fileUploadService;

	/**
	 * 发表动态
	 */
	@RequestMapping(value = "/send", method = POST)
	public JsonResponse addSharing(@RequestBody SharingForm sharingForm, @RequestHeader String openid,
			HttpSession session) {
		Member member = (Member) session.getAttribute("loginer");
		if (member == null) {
			// 尝试从Header里读取openid
			member = memberMapper.findByOpenid(openid);
			if (member == null) {
				return JsonResponse.custom(INVALID_TOKEN);
			}
		}

		// 数据校验
		if (sharingForm.stopCreate()) {
			return JsonResponse.invalid("缺少必输项！");
		}

		try {
			log.debug("media_id=" + sharingForm.getMedia_id());
			// 下载图片并保存到七牛服务器上
			String imageUrl = fileUploadService.upload(sharingForm.getMedia_id());

			// 新增一条动态
			Sharing sharing = sharingTransfer.toEntity(sharingForm);
			sharing.setImageUrl(imageUrl);
			sharing.setMemberId(member.getId());
			sharing.setNickName(member.getNickName());
			sharing.setHeadImgurl(member.getHeadImgurl());
			sharingService.add(sharing);
			return JsonResponse.ok();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return JsonResponse.custom(EXCEPTION);
		}
	}

	/**
	 * 查询动态
	 */
	@RequestMapping(method = GET)
	public JsonResponse getPage(SharingQuery query) {
		try {
			PageHelper.startPage(query.pageWhitoutOrder());
			List<Sharing> sharings = sharingMapper.findAll();
			return JsonResponse.ok(new PageInfo<>(sharings));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return JsonResponse.custom(EXCEPTION);
		}
	}
}
